﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace HYRElectric.WirePlace
{
	public partial class DistanceSetupForm : HYBaseForm
	{
		public DistanceSetupForm(WindowHandle hWndRevit)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.m_hWndRevit = hWndRevit;
		}

		private void DistanceSetupForm_Load(object sender, EventArgs e)
		{
			this.m_txtDistance.Text = this.userSetting.GetDistanceSetup();
		}

		public int GetDistance()
		{
			return Convert.ToInt32(this.m_txtDistance.Text);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DistanceSetupForm.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DistanceSetupForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			DistanceSetupForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void DistanceSetupForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.userSetting.SaveDistanceSetup(this.m_txtDistance.Text);
			this.KeyEsc();
		}

		private WindowHandle m_hWndRevit;

		private HVACUserSetting userSetting = new HVACUserSetting();
	}
}
